package dao

import "gorm.io/gorm"

func (i *internalDao[Ent]) Association(model *Ent, column string, cds ...any) (IAssociation, error) {
	// db, err := i.makeScopes(i.db.Model(model), cds...)
	db, err := useScopes[Ent](i.db.Model(model), true, cds...)
	if err != nil {
		return nil, err
	}

	return withAssociation(db.Association(column)), nil
}

var _ IAssociation = (*Association)(nil)

func withAssociation(db *gorm.Association) IAssociation {
	return &Association{db}
}

type Association struct {
	db *gorm.Association
}

// Append implements IAssociation.
func (a *Association) Append(vals ...any) error {
	return a.db.Append(vals...)
}

// Delete implements IAssociation.
func (a *Association) Delete(vals ...any) error {
	return a.db.Delete(vals...)
}

// Replace implements IAssociation.
func (a *Association) Replace(vals ...any) error {
	return a.db.Replace(vals...)
}

func (a *Association) Find(output any, query any, args ...any) error {
	return a.db.Find(output, append([]any{query}, args...)...)
}

func (a *Association) Clear() error {
	return a.db.Clear()
}

func (a *Association) Count() int64 {
	return a.db.Count()
}
